package com.kyleswebspace.propertyanalyzer.gui;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.BorderBox;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class FinancingPanel extends JPanel implements Updateable, KeyListener,
		MouseListener, ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3835337980986310336L;

	private DataModel dm;
	private FocusMediator fm;

	private JLabel mort1Label, mort2Label, mort3Label, totalLabel;
	private JLabel percentPPLabel;
	private JTextField percentPPField;
	private JTextField percentPPFieldB;
	private JTextField percentPPFieldC;
	private JTextField percentPPFieldD;
	private JLabel loanAmountLabel;
	private JTextField loanAmountField;
	private JTextField loanAmountFieldB;
	private JTextField loanAmountFieldC;
	private JTextField loanAmountFieldD;
	private JLabel loanToValueLabel;
	private JTextField loanToValueField;
	private JTextField loanToValueFieldB;
	private JTextField loanToValueFieldC;
	private JLabel paymentTypeLabel;
	private JComboBox paymentTypeBoxA;
	private JComboBox paymentTypeBoxB;
	private JComboBox paymentTypeBoxC;
	private JLabel amortizationLabel;
	private JTextField amortizationField;
	private JTextField amortizationFieldB;
	private JTextField amortizationFieldC;
	private JLabel interestRateLabel;
	private JTextField interestRateField;
	private JTextField interestRateFieldB;
	private JTextField interestRateFieldC;
	private JLabel mInterestRateLabel;
	private JTextField mInterestRateField;
	private JTextField mInterestRateFieldB;
	private JTextField mInterestRateFieldC;
	private JLabel applyInsuranceLabel;
	private JComboBox applyInsuranceBoxA;
	private JComboBox applyInsuranceBoxB;
	private JComboBox applyInsuranceBoxC;
	private JLabel mortgagePremLabel;
	private JTextField mortgagePremField;
	private JTextField mortgagePremFieldB;
	private JTextField mortgagePremFieldC;
	private JLabel cmhcFeeLabel;
	private JTextField cmhcFeeField;
	private JTextField cmhcFeeFieldB;
	private JTextField cmhcFeeFieldC;
	private JTextField cmhcFeeFieldD;
	private JLabel year1PPDLabel;
	private JTextField year1PPDField;
	private JTextField year1PPDFieldB;
	private JTextField year1PPDFieldC;
	private JTextField year1PPDFieldD;
	private JLabel monthlyPaymentLabel;
	private JTextField monthlyPaymentField;
	private JTextField monthlyPaymentFieldB;
	private JTextField monthlyPaymentFieldC;
	private JTextField monthlyPaymentFieldD;

	private final Color editableColor = ColorBox.fieldColor;
	private final Color overridableColor = ColorBox.overridableColor;
	private final Color overriddenColor = ColorBox.overriddenColor;
	private Color innerPanelColor = ColorBox.innerPanelColor;
	private Font appFont = ArcaFont.appFont;
	private Font comboBoxFont = ArcaFont.comboBoxFont;

	public FinancingPanel(DataModel dm, FocusMediator fm) {
		this.dm = dm;
		this.fm = fm;
		dm.register(this);

		this.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Financing");
		this.setBorder(title);
		this.setBackground(innerPanelColor);

		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20; // PADDING

		// top labels
		mort1Label = new JLabel("Mort #1 ");
		mort1Label.setFont(appFont);
		mort1Label.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 3;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mort1Label, constraints);
		mort2Label = new JLabel("Mort #2 ");
		mort2Label.setFont(appFont);
		mort2Label.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 4;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mort2Label, constraints);
		mort3Label = new JLabel("Mort #3 ");
		mort3Label.setFont(appFont);
		mort3Label.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 5;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mort3Label, constraints);
		totalLabel = new JLabel("Total ");
		totalLabel.setFont(appFont);
		totalLabel.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 6;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.CENTER;
		this.add(totalLabel, constraints);

		// % purchase price
		percentPPLabel = new JLabel("% of Purchase Price");
		percentPPLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(percentPPLabel, constraints);
		percentPPField = new JTextField(2);
		fm.setPercentPPAField(percentPPField);
		percentPPField.addFocusListener(this);
		percentPPField.addKeyListener(this);
		percentPPField.setFont(appFont);
		percentPPField.setBorder(BorderBox.fieldBorder);
		percentPPField
				.setToolTipText("<html>How much money do you want from the bank?<br/><br/>"
						+ "Under four units you can ask for up to<br/>"
						+ "80% without adding insurance.<br/>"
						+ "Over four units you can only get 60 or<br/>"
						+ "65% without insurance.<br/>"
						+ "For values greater than these you will<br/>"
						+ "need mortgage insurance and therefore<br/>"
						+ "click yes in 'Apply for Mortgage<br/>"
						+ "Insurance'.</html>");
		percentPPFieldB = new JTextField(2);
		fm.setPercentPPBField(percentPPFieldB);
		percentPPFieldB.setBackground(editableColor);
		percentPPFieldB.addFocusListener(this);
		percentPPFieldB.addKeyListener(this);
		percentPPFieldB.setFont(appFont);
		percentPPFieldC = new JTextField(2);
		fm.setPercentPPCField(percentPPFieldC);
		percentPPFieldC.setBackground(editableColor);
		percentPPFieldC.addFocusListener(this);
		percentPPFieldC.addKeyListener(this);
		percentPPFieldC.setFont(appFont);
		percentPPFieldD = new JTextField(2);
		percentPPFieldD.addFocusListener(this);
		percentPPFieldD.setFocusable(false);
		percentPPFieldD.setFont(appFont);
		percentPPField.setHorizontalAlignment(JTextField.RIGHT);
		percentPPFieldB.setHorizontalAlignment(JTextField.RIGHT);
		percentPPFieldC.setHorizontalAlignment(JTextField.RIGHT);
		percentPPFieldD.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		percentPPField.setBackground(editableColor);
		this.add(percentPPField, constraints);
		constraints.gridx = 4;
		this.add(percentPPFieldB, constraints);
		constraints.gridx = 5;
		this.add(percentPPFieldC, constraints);
		constraints.gridx = 6;
		this.add(percentPPFieldD, constraints);

		// loan amount
		loanAmountLabel = new JLabel("Loan amount");
		loanAmountLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(loanAmountLabel, constraints);
		loanAmountField = new JTextField(4);
		fm.setLoanAmountAField(loanAmountField);
		loanAmountField.addFocusListener(this);
		loanAmountField.addMouseListener(this);
		loanAmountField.addKeyListener(this);
		loanAmountField.setFont(appFont);
		loanAmountField.setBackground(overridableColor);
		loanAmountField.setBorder(BorderBox.fieldBorder);
		loanAmountField
				.setToolTipText("<html>This value will automatically<br/>"
						+ "be created based upon the<br/>"
						+ "above percentage and the<br/>"
						+ "purchase price you entered.</html>");
		loanAmountFieldB = new JTextField(4);
		fm.setLoanAmountBField(loanAmountFieldB);
		loanAmountFieldB.addFocusListener(this);
		loanAmountFieldB.addMouseListener(this);
		loanAmountFieldB.addKeyListener(this);
		loanAmountFieldB.setFont(appFont);
		loanAmountFieldB.setBackground(overridableColor);
		loanAmountFieldC = new JTextField(4);
		fm.setLoanAmountCField(loanAmountFieldC);
		loanAmountFieldC.addFocusListener(this);
		loanAmountFieldC.addMouseListener(this);
		loanAmountFieldC.addKeyListener(this);
		loanAmountFieldC.setFont(appFont);
		loanAmountFieldC.setBackground(overridableColor);
		loanAmountFieldD = new JTextField(4);
		loanAmountFieldD.addFocusListener(this);
		loanAmountFieldD.setFocusable(false);
		loanAmountFieldD.setFont(appFont);
		loanAmountField.setHorizontalAlignment(JTextField.RIGHT);
		loanAmountFieldB.setHorizontalAlignment(JTextField.RIGHT);
		loanAmountFieldC.setHorizontalAlignment(JTextField.RIGHT);
		loanAmountFieldD.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(loanAmountField, constraints);
		constraints.gridx = 4;
		this.add(loanAmountFieldB, constraints);
		constraints.gridx = 5;
		this.add(loanAmountFieldC, constraints);
		constraints.gridx = 6;
		this.add(loanAmountFieldD, constraints);

		// loan to value
		loanToValueLabel = new JLabel("Loan to Value (LTV)");
		loanToValueLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 3;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(loanToValueLabel, constraints);
		loanToValueField = new JTextField(2);
		loanToValueField.addFocusListener(this);
		loanToValueField.setFocusable(false);
		loanToValueField.setFont(appFont);
		loanToValueFieldB = new JTextField(2);
		loanToValueFieldB.addFocusListener(this);
		loanToValueFieldB.setFocusable(false);
		loanToValueFieldB.setFont(appFont);
		loanToValueFieldC = new JTextField(2);
		loanToValueFieldC.addFocusListener(this);
		loanToValueFieldC.setFocusable(false);
		loanToValueFieldC.setFont(appFont);
		loanToValueField.setHorizontalAlignment(JTextField.RIGHT);
		loanToValueFieldB.setHorizontalAlignment(JTextField.RIGHT);
		loanToValueFieldC.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(loanToValueField, constraints);
		constraints.gridx = 4;
		this.add(loanToValueFieldB, constraints);
		constraints.gridx = 5;
		this.add(loanToValueFieldC, constraints);

		// payment type
		paymentTypeLabel = new JLabel("Payment Type");
		paymentTypeLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(paymentTypeLabel, constraints);

		String[] choices = { "pri+int", "int" };
		paymentTypeBoxA = new JComboBox(choices);
		fm.setPaymentTypeAField(paymentTypeBoxA);
		paymentTypeBoxA.addFocusListener(this);
		paymentTypeBoxA.addKeyListener(this);
		paymentTypeBoxA.addItemListener(this);
		paymentTypeBoxA.setFont(appFont);
		paymentTypeBoxA.setBorder(BorderBox.fieldBorder);
		paymentTypeBoxA
				.setToolTipText("<html>If this mortgage is made up of a<br/>"
						+ "principle and interest payment<br/>"
						+ "then you will leave it as<br/>"
						+ "\"Pri+Int\".<br/>"
						+ "If this mortgage payent is<br/>"
						+ "interest only you would then<br/>"
						+ "click \"Int\".</html>");
		paymentTypeBoxB = new JComboBox(choices);
		fm.setPaymentTypeBField(paymentTypeBoxB);
		paymentTypeBoxB.addFocusListener(this);
		paymentTypeBoxB.addKeyListener(this);
		paymentTypeBoxB.addItemListener(this);
		paymentTypeBoxB.setFont(appFont);
		paymentTypeBoxC = new JComboBox(choices);
		fm.setPaymentTypeCField(paymentTypeBoxC);
		paymentTypeBoxC.addFocusListener(this);
		paymentTypeBoxC.addKeyListener(this);
		paymentTypeBoxC.addItemListener(this);
		paymentTypeBoxC.setFont(appFont);
		paymentTypeBoxA.setPreferredSize(new Dimension(45, loanToValueField
				.getPreferredSize().height + 5));
		paymentTypeBoxB.setPreferredSize(new Dimension(45, loanToValueField
				.getPreferredSize().height + 5));
		paymentTypeBoxC.setPreferredSize(new Dimension(45, loanToValueField
				.getPreferredSize().height + 5));
		paymentTypeBoxB.setBackground(editableColor);
		paymentTypeBoxC.setBackground(editableColor);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		paymentTypeBoxA.setBackground(editableColor);
		this.add(paymentTypeBoxA, constraints);
		constraints.gridx = 4;
		this.add(paymentTypeBoxB, constraints);
		constraints.gridx = 5;
		this.add(paymentTypeBoxC, constraints);

		// amortization
		amortizationLabel = new JLabel("Amortization");
		amortizationLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 5;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(amortizationLabel, constraints);
		amortizationField = new JTextField(2);
		fm.setAmortizationAField(amortizationField);
		amortizationField.addFocusListener(this);
		amortizationField.addKeyListener(this);
		amortizationField.setFont(appFont);
		amortizationField.setBorder(BorderBox.fieldBorder);
		amortizationField
				.setToolTipText("<html>This value is the total life of the<br/>"
						+ "mortgage and for new mortgages the<br/>"
						+ "standard length of time is 25 years.<br/>"
						+ "You can get 30 and 35 year terms<br/>"
						+ "which will lower your monthly payment<br/>"
						+ "but if you are applying for mortgage<br/>"
						+ "insurance then the premium will<br/>"
						+ "increase slightly. If you have an<br/>"
						+ "interest only payment for your<br/>"
						+ "mortgage then you do not need a value<br/>"
						+ "in this cell.</html>");
		amortizationFieldB = new JTextField(2);
		fm.setAmortizationBField(amortizationFieldB);
		amortizationFieldB.addFocusListener(this);
		amortizationFieldB.addKeyListener(this);
		amortizationFieldB.setFont(appFont);
		amortizationFieldC = new JTextField(2);
		fm.setAmortizationCField(amortizationFieldC);
		amortizationFieldC.addFocusListener(this);
		amortizationFieldC.addKeyListener(this);
		amortizationFieldC.setFont(appFont);
		amortizationField.setHorizontalAlignment(JTextField.RIGHT);
		amortizationFieldB.setHorizontalAlignment(JTextField.RIGHT);
		amortizationFieldC.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		amortizationField.setBackground(editableColor);
		amortizationFieldB.setBackground(editableColor);
		amortizationFieldC.setBackground(editableColor);
		this.add(amortizationField, constraints);
		constraints.gridx = 4;
		this.add(amortizationFieldB, constraints);
		constraints.gridx = 5;
		this.add(amortizationFieldC, constraints);

		// interest rate
		interestRateLabel = new JLabel("Interest Rate");
		interestRateLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 6;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(interestRateLabel, constraints);
		interestRateField = new JTextField(2);
		fm.setInterestRateAField(interestRateField);
		interestRateField.addFocusListener(this);
		interestRateField.addKeyListener(this);
		interestRateField.setFont(appFont);
		interestRateField.setBorder(BorderBox.fieldBorder);
		interestRateField
				.setToolTipText("<html>This will be the interest rate<br/>"
						+ "your lender will be charging<br/>"
						+ "you. You will need to check<br/>"
						+ "and see what the current<br/>"
						+ "rates are each time you<br/>"
						+ "analyze a property.</html>");
		interestRateFieldB = new JTextField(2);
		fm.setInterestRateBField(interestRateFieldB);
		interestRateFieldB.addFocusListener(this);
		interestRateFieldB.addKeyListener(this);
		interestRateFieldB.setFont(appFont);
		interestRateFieldC = new JTextField(2);
		fm.setInterestRateCField(interestRateFieldC);
		interestRateFieldC.addFocusListener(this);
		interestRateFieldC.addKeyListener(this);
		interestRateFieldC.setFont(appFont);
		interestRateField.setHorizontalAlignment(JTextField.RIGHT);
		interestRateFieldB.setHorizontalAlignment(JTextField.RIGHT);
		interestRateFieldC.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		interestRateField.setBackground(editableColor);
		interestRateFieldB.setBackground(editableColor);
		interestRateFieldC.setBackground(editableColor);
		this.add(interestRateField, constraints);
		constraints.gridx = 4;
		this.add(interestRateFieldB, constraints);
		constraints.gridx = 5;
		this.add(interestRateFieldC, constraints);

		// monthly interest rate
		mInterestRateLabel = new JLabel("Monthly Interest Rate");
		mInterestRateLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 7;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(mInterestRateLabel, constraints);
		mInterestRateField = new JTextField(2);
		mInterestRateField.addFocusListener(this);
		mInterestRateField.setFocusable(false);
		mInterestRateField.setFont(appFont);
		mInterestRateFieldB = new JTextField(2);
		mInterestRateFieldB.addFocusListener(this);
		mInterestRateFieldB.setFocusable(false);
		mInterestRateFieldB.setFont(appFont);
		mInterestRateFieldC = new JTextField(2);
		mInterestRateFieldC.addFocusListener(this);
		mInterestRateFieldC.setFocusable(false);
		mInterestRateFieldC.setFont(appFont);
		mInterestRateField.setHorizontalAlignment(JTextField.RIGHT);
		mInterestRateFieldB.setHorizontalAlignment(JTextField.RIGHT);
		mInterestRateFieldC.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mInterestRateField, constraints);
		constraints.gridx = 4;
		this.add(mInterestRateFieldB, constraints);
		constraints.gridx = 5;
		this.add(mInterestRateFieldC, constraints);

		// apply for mortgage insurance (yes/no)
		applyInsuranceLabel = new JLabel("Apply for Mortgage Insurance");
		applyInsuranceLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 8;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(applyInsuranceLabel, constraints);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;

		String[] responses = { "yes", "no" };
		applyInsuranceBoxA = new JComboBox(responses);
		fm.setApplyMortInsFieldA(applyInsuranceBoxA);
		applyInsuranceBoxA.addFocusListener(this);
		applyInsuranceBoxA.addKeyListener(this);
		applyInsuranceBoxA.addItemListener(this);
		applyInsuranceBoxA.setFont(comboBoxFont);
		applyInsuranceBoxA.setMinimumSize(new Dimension(1, 1));
		applyInsuranceBoxA.setPreferredSize(new Dimension(25,
				mInterestRateField.getPreferredSize().height + 5));
		applyInsuranceBoxA.setBorder(BorderBox.fieldBorder);
		applyInsuranceBoxA
				.setToolTipText("<html>Click yes if the percentage of your<br/>"
						+ "mortgage is greater than 80% for<br/>"
						+ "properties with four doors or less and<br/>"
						+ "yes for properties with more than<br/>"
						+ "four doors if the mortgage value is<br/>"
						+ "greater than 60% of the purchase<br/>"
						+ "price.</html>");
		applyInsuranceBoxB = new JComboBox(responses);
		fm.setApplyMortInsFieldB(applyInsuranceBoxB);
		applyInsuranceBoxB.addFocusListener(this);
		applyInsuranceBoxB.addKeyListener(this);
		applyInsuranceBoxB.addItemListener(this);
		applyInsuranceBoxB.setFont(comboBoxFont);
		applyInsuranceBoxB.setMinimumSize(new Dimension(1, 1));
		applyInsuranceBoxB.setPreferredSize(new Dimension(25,
				mInterestRateField.getPreferredSize().height + 5));
		applyInsuranceBoxB.setBorder(BorderBox.fieldBorder);
		applyInsuranceBoxC = new JComboBox(responses);
		fm.setApplyMortInsFieldC(applyInsuranceBoxC);
		applyInsuranceBoxC.addFocusListener(this);
		applyInsuranceBoxC.addKeyListener(this);
		applyInsuranceBoxC.addItemListener(this);
		applyInsuranceBoxC.setFont(comboBoxFont);
		applyInsuranceBoxC.setMinimumSize(new Dimension(1, 1));
		applyInsuranceBoxC.setPreferredSize(new Dimension(25,
				mInterestRateField.getPreferredSize().height + 5));
		applyInsuranceBoxC.setBorder(BorderBox.fieldBorder);
		applyInsuranceBoxA.setBackground(editableColor);
		applyInsuranceBoxB.setBackground(editableColor);
		applyInsuranceBoxC.setBackground(editableColor);
		// this.add(applyInsuranceField, constraints);
		this.add(applyInsuranceBoxA, constraints);
		constraints.gridx = 4;
		this.add(applyInsuranceBoxB, constraints);
		constraints.gridx = 5;
		this.add(applyInsuranceBoxC, constraints);

		// mortgage premium
		mortgagePremLabel = new JLabel("Mortgage Premium");
		mortgagePremLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 9;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(mortgagePremLabel, constraints);
		mortgagePremField = new JTextField(2);
		mortgagePremField.addFocusListener(this);
		mortgagePremField.setFocusable(false);
		mortgagePremField.setFont(appFont);
		mortgagePremFieldB = new JTextField(2);
		mortgagePremFieldB.addFocusListener(this);
		mortgagePremFieldB.setFocusable(false);
		mortgagePremFieldB.setFont(appFont);
		mortgagePremFieldC = new JTextField(2);
		mortgagePremFieldC.addFocusListener(this);
		mortgagePremFieldC.setFocusable(false);
		mortgagePremFieldC.setFont(appFont);
		mortgagePremField.setHorizontalAlignment(JTextField.RIGHT);
		mortgagePremFieldB.setHorizontalAlignment(JTextField.RIGHT);
		mortgagePremFieldC.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(mortgagePremField, constraints);
		constraints.gridx = 4;
		this.add(mortgagePremFieldB, constraints);
		constraints.gridx = 5;
		this.add(mortgagePremFieldC, constraints);

		// CMHC fee
		cmhcFeeLabel = new JLabel("CMHC Fee");
		cmhcFeeLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 10;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(cmhcFeeLabel, constraints);
		cmhcFeeField = new JTextField(4);
		fm.setCmhcFeeFieldA(cmhcFeeField);
		cmhcFeeField.addFocusListener(this);
		cmhcFeeField.addMouseListener(this);
		cmhcFeeField.setFont(appFont);
		cmhcFeeField.setBackground(overridableColor);
		cmhcFeeFieldB = new JTextField(4);
		fm.setCmhcFeeFieldB(cmhcFeeFieldB);
		cmhcFeeFieldB.addFocusListener(this);
		cmhcFeeFieldB.addMouseListener(this);
		cmhcFeeFieldB.setFont(appFont);
		cmhcFeeFieldB.setBackground(overridableColor);
		cmhcFeeFieldC = new JTextField(4);
		fm.setCmhcFeeFieldC(cmhcFeeFieldC);
		cmhcFeeFieldC.addFocusListener(this);
		cmhcFeeFieldC.addMouseListener(this);
		cmhcFeeFieldC.setFont(appFont);
		cmhcFeeFieldC.setBackground(overridableColor);
		cmhcFeeFieldD = new JTextField(4);
		cmhcFeeFieldD.addFocusListener(this);
		cmhcFeeFieldD.setFocusable(false);
		cmhcFeeFieldD.setFont(appFont);
		cmhcFeeFieldD.setToolTipText("<html>Most mortgage insurance fees<br/>"
				+ "will be added to the principal of<br/>"
				+ "the mortgage for repayment but<br/>"
				+ "this total must be expensed over<br/>"
				+ "a 5 year period max. Discuss<br/>"
				+ "with your accountant.</html>");
		cmhcFeeFieldD.setBorder(BorderBox.fieldBorder);
		cmhcFeeField.setHorizontalAlignment(JTextField.RIGHT);
		cmhcFeeFieldB.setHorizontalAlignment(JTextField.RIGHT);
		cmhcFeeFieldC.setHorizontalAlignment(JTextField.RIGHT);
		cmhcFeeFieldD.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(cmhcFeeField, constraints);
		constraints.gridx = 4;
		this.add(cmhcFeeFieldB, constraints);
		constraints.gridx = 5;
		this.add(cmhcFeeFieldC, constraints);
		constraints.gridx = 6;
		this.add(cmhcFeeFieldD, constraints);

		// year 1 principal paydown
		year1PPDLabel = new JLabel("Year 1 Principal Paydown (PPD)");
		year1PPDLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 11;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(year1PPDLabel, constraints);
		year1PPDField = new JTextField(4);
		year1PPDField.addFocusListener(this);
		year1PPDField.setFocusable(false);
		year1PPDField.setFont(appFont);
		year1PPDFieldB = new JTextField(4);
		year1PPDFieldB.addFocusListener(this);
		year1PPDFieldB.setFocusable(false);
		year1PPDFieldB.setFont(appFont);
		year1PPDFieldC = new JTextField(4);
		year1PPDFieldC.addFocusListener(this);
		year1PPDFieldC.setFocusable(false);
		year1PPDFieldC.setFont(appFont);
		year1PPDFieldD = new JTextField(4);
		year1PPDFieldD.addFocusListener(this);
		year1PPDFieldD.setFocusable(false);
		year1PPDFieldD.setFont(appFont);
		year1PPDFieldD.setToolTipText("<html>This is the principal paid<br/>"
				+ "down in the first year on<br/>" + "all mortgages.</html>");
		year1PPDFieldD.setBorder(BorderBox.fieldBorder);
		year1PPDField.setHorizontalAlignment(JTextField.RIGHT);
		year1PPDFieldB.setHorizontalAlignment(JTextField.RIGHT);
		year1PPDFieldC.setHorizontalAlignment(JTextField.RIGHT);
		year1PPDFieldD.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(year1PPDField, constraints);
		constraints.gridx = 4;
		this.add(year1PPDFieldB, constraints);
		constraints.gridx = 5;
		this.add(year1PPDFieldC, constraints);
		constraints.gridx = 6;
		this.add(year1PPDFieldD, constraints);

		// separator
		constraints.gridx = 0;
		constraints.gridwidth = 7;
		constraints.gridy = 12;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(new JSeparator(SwingConstants.HORIZONTAL), constraints);
		constraints.fill = GridBagConstraints.NONE;

		// monthly payment
		monthlyPaymentLabel = new JLabel("Monthly Payment");
		monthlyPaymentLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 13;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(monthlyPaymentLabel, constraints);
		monthlyPaymentField = new JTextField(4);
		monthlyPaymentField.addFocusListener(this);
		monthlyPaymentField.setFocusable(false);
		monthlyPaymentField.setFont(appFont);
		monthlyPaymentFieldB = new JTextField(4);
		monthlyPaymentFieldB.addFocusListener(this);
		monthlyPaymentFieldB.setFocusable(false);
		monthlyPaymentFieldB.setFont(appFont);
		monthlyPaymentFieldC = new JTextField(4);
		monthlyPaymentFieldC.addFocusListener(this);
		monthlyPaymentFieldC.setFocusable(false);
		monthlyPaymentFieldC.setFont(appFont);
		monthlyPaymentFieldD = new JTextField(4);
		monthlyPaymentFieldD.addFocusListener(this);
		monthlyPaymentFieldD.setFocusable(false);
		monthlyPaymentFieldD.setFont(appFont);
		monthlyPaymentField.setHorizontalAlignment(JTextField.RIGHT);
		monthlyPaymentFieldB.setHorizontalAlignment(JTextField.RIGHT);
		monthlyPaymentFieldC.setHorizontalAlignment(JTextField.RIGHT);
		monthlyPaymentFieldD.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(monthlyPaymentField, constraints);
		constraints.gridx = 4;
		this.add(monthlyPaymentFieldB, constraints);
		constraints.gridx = 5;
		this.add(monthlyPaymentFieldC, constraints);
		constraints.gridx = 6;
		this.add(monthlyPaymentFieldD, constraints);

		//update overridden values, if any
		if(dm.isOverrideLoanAmountA()) {
			loanAmountField.setBackground(overriddenColor);
		}
		if(dm.isOverrideLoanAmountB()) {
			loanAmountFieldB.setBackground(overriddenColor);
		}	
		if(dm.isOverrideLoanAmountC()) {
			loanAmountFieldC.setBackground(overriddenColor);
		}	
		if(dm.isOverrideCMHCA()) {
			cmhcFeeField.setBackground(overriddenColor);
		}
		if(dm.isOverrideCMHCB()) {
			cmhcFeeFieldB.setBackground(overriddenColor);
		}	
		if(dm.isOverrideCMHCC()) {
			cmhcFeeFieldC.setBackground(overriddenColor);
		}	
		
		updateFields();
	}
	
	public void updateModel() {
		dm.setPercentPPA(FieldParser
				.parseDecimal(this.percentPPField.getText()));
		dm.setPercentPPB(FieldParser.parseDecimal(this.percentPPFieldB
				.getText()));
		dm.setPercentPPC(FieldParser.parseDecimal(this.percentPPFieldC
				.getText()));
		dm.setPaymentTypeA((String) (this.paymentTypeBoxA.getSelectedItem()));
		dm.setPaymentTypeB((String) (this.paymentTypeBoxB.getSelectedItem()));
		dm.setPaymentTypeC((String) (this.paymentTypeBoxC.getSelectedItem()));
		dm.setAmortizationA(FieldParser.parseInteger(this.amortizationField
				.getText()));
		dm.setAmortizationB(FieldParser.parseInteger(this.amortizationFieldB
				.getText()));
		dm.setAmortizationC(FieldParser.parseInteger(this.amortizationFieldC
				.getText()));
		dm.setInterestRateA(FieldParser.parseDecimal(this.interestRateField
				.getText()));
		dm.setInterestRateB(FieldParser.parseDecimal(this.interestRateFieldB
				.getText()));
		dm.setInterestRateC(FieldParser.parseDecimal(this.interestRateFieldC
				.getText()));
		dm.setApplyMortgageInsuranceA((String) (this.applyInsuranceBoxA
				.getSelectedItem()));
		dm.setApplyMortgageInsuranceB((String) (this.applyInsuranceBoxB
				.getSelectedItem()));
		dm.setApplyMortgageInsuranceC((String) (this.applyInsuranceBoxC
				.getSelectedItem()));

		if (dm.isOverrideLoanAmountA()) {
			dm.setLoanAmountA(FieldParser.parseInteger(this.loanAmountField
					.getText()));
		}
		if (dm.isOverrideLoanAmountB()) {
			dm.setLoanAmountB(FieldParser.parseInteger(this.loanAmountFieldB
					.getText()));
		}
		if (dm.isOverrideLoanAmountC()) {
			dm.setLoanAmountC(FieldParser.parseInteger(this.loanAmountFieldC
					.getText()));
		}
		if (dm.isOverrideCMHCA()) {
			dm.setCmhcFeeA(FieldParser
					.parseInteger(this.cmhcFeeField.getText()));
		}
		if (dm.isOverrideCMHCB()) {
			dm.setCmhcFeeB(FieldParser.parseInteger(this.cmhcFeeFieldB
					.getText()));
		}
		if (dm.isOverrideCMHCC()) {
			dm.setCmhcFeeC(FieldParser.parseInteger(this.cmhcFeeFieldC
					.getText()));
		}

		dm.updateInterface();		
	}

	public void updateFields() {
		this.percentPPField.setText(dm.getPercentPPA() + "%");
		this.percentPPFieldB.setText(dm.getPercentPPB() + "%");
		this.percentPPFieldC.setText(dm.getPercentPPC() + "%");
		this.percentPPFieldD.setText(dm.getPercentPPTotal());
		this.loanAmountField.setText(dm.getLoanAmountA());
		this.loanAmountFieldB.setText(dm.getLoanAmountB());
		this.loanAmountFieldC.setText(dm.getLoanAmountC());
		this.loanAmountFieldD.setText(dm.getLoanAmountTotal());
		this.loanToValueField.setText(dm.getLoanToValueA()
				+ ((dm.getLoanToValueA().equalsIgnoreCase("n/a")) ? "" : "%"));
		this.loanToValueFieldB.setText(dm.getLoanToValueB()
				+ ((dm.getLoanToValueB().equalsIgnoreCase("n/a")) ? "" : "%"));
		this.loanToValueFieldC.setText(dm.getLoanToValueC()
				+ ((dm.getLoanToValueC().equalsIgnoreCase("n/a")) ? "" : "%"));
		this.paymentTypeBoxA.setSelectedIndex(dm.getPaymentTypeA()
				.equalsIgnoreCase("pri+int") ? 0 : 1);
		this.paymentTypeBoxB.setSelectedIndex(dm.getPaymentTypeB()
				.equalsIgnoreCase("pri+int") ? 0 : 1);
		this.paymentTypeBoxC.setSelectedIndex(dm.getPaymentTypeC()
				.equalsIgnoreCase("pri+int") ? 0 : 1);
		this.amortizationField.setText(dm.getAmortizationA() + " yrs");
		this.amortizationFieldB.setText(dm.getAmortizationB() + " yrs");
		this.amortizationFieldC.setText(dm.getAmortizationC() + " yrs");
		this.interestRateField.setText(dm.getInterestRateA() + "%");
		this.interestRateFieldB.setText(dm.getInterestRateB() + "%");
		this.interestRateFieldC.setText(dm.getInterestRateC() + "%");
		this.mInterestRateField.setText(dm.getMonthlyIntRateA() + "%");
		this.mInterestRateFieldB.setText(dm.getMonthlyIntRateB() + "%");
		this.mInterestRateFieldC.setText(dm.getMonthlyIntRateC() + "%");
		this.applyInsuranceBoxA.setSelectedIndex(dm
				.getApplyMortgageInsuranceA().equalsIgnoreCase("yes") ? 0 : 1);
		this.applyInsuranceBoxB.setSelectedIndex(dm
				.getApplyMortgageInsuranceB().equalsIgnoreCase("yes") ? 0 : 1);
		this.applyInsuranceBoxC.setSelectedIndex(dm
				.getApplyMortgageInsuranceC().equalsIgnoreCase("yes") ? 0 : 1);
		this.mortgagePremField.setText(dm.getMortgagePremiumA() + "%");
		this.mortgagePremFieldB.setText(dm.getMortgagePremiumB() + "%");
		this.mortgagePremFieldC.setText(dm.getMortgagePremiumC() + "%");
		this.cmhcFeeField.setText(dm.getCmhcFeeA());
		this.cmhcFeeFieldB.setText(dm.getCmhcFeeB());
		this.cmhcFeeFieldC.setText(dm.getCmhcFeeC());
		this.cmhcFeeFieldD.setText(dm.getCmhcFeeTotal());
		this.year1PPDField.setText(dm.getYear1PPDA());
		this.year1PPDFieldB.setText(dm.getYear1PPDB());
		this.year1PPDFieldC.setText(dm.getYear1PPDC());
		this.year1PPDFieldD.setText(dm.getYear1PPDTotal());
		this.monthlyPaymentField.setText(dm.getMonthlyPaymentA());
		this.monthlyPaymentFieldB.setText(dm.getMonthlyPaymentB());
		this.monthlyPaymentFieldC.setText(dm.getMonthlyPaymentC());
		this.monthlyPaymentFieldD.setText(dm.getMonthlyPaymentTotal());

		if (dm.isOverrideLoanAmountA())
			loanAmountField.setBackground(ColorBox.overriddenColor);
		if (dm.isOverrideLoanAmountB())
			loanAmountFieldB.setBackground(ColorBox.overriddenColor);
		if (dm.isOverrideLoanAmountC())
			loanAmountFieldC.setBackground(ColorBox.overriddenColor);
		if (dm.isOverrideCMHCA())
			cmhcFeeField.setBackground(ColorBox.overriddenColor);
		if (dm.isOverrideCMHCB())
			cmhcFeeFieldB.setBackground(ColorBox.overriddenColor);
		if (dm.isOverrideCMHCC())
			cmhcFeeFieldC.setBackground(ColorBox.overriddenColor);
	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() instanceof JTextField) {
			JTextField focusedField = (JTextField) e.getSource();
			focusedField.selectAll();
		}

	}

	@Override
	public void focusLost(FocusEvent e) {
		updateModel();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getSource() == percentPPField) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getPaymentTypeAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPaybackPeriodField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getRentField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getPercentPPBField().requestFocus();
			}
		} else if (e.getSource() == percentPPFieldB) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getPaymentTypeBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPaybackPeriodField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getPercentPPAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getPercentPPCField().requestFocus();
			}
		} else if (e.getSource() == percentPPFieldC) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getPaymentTypeCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getApplyMortInsFieldC().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getPercentPPBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAddressField().requestFocus();
			}
		} else if (e.getSource() == paymentTypeBoxA) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getAmortizationAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPercentPPAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getOtherField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getPaymentTypeBField().requestFocus();
			}
		} else if (e.getSource() == paymentTypeBoxB) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getAmortizationBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPercentPPBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getPaymentTypeAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getPaymentTypeCField().requestFocus();
			}
		} else if (e.getSource() == paymentTypeBoxC) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getAmortizationCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPercentPPCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getPaymentTypeBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAddressField().requestFocus();
			}
		} else if (e.getSource() == amortizationField) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getInterestRateAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPaymentTypeAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getOtherField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAmortizationBField().requestFocus();
			}
		} else if (e.getSource() == amortizationFieldB) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getInterestRateBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPaymentTypeBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getAmortizationAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAmortizationCField().requestFocus();
			}
		} else if (e.getSource() == amortizationFieldC) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getInterestRateCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getPaymentTypeCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getAmortizationBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAddressField().requestFocus();
			}
		} else if (e.getSource() == interestRateField) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getApplyMortInsFieldA().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getAmortizationAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getRentField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getInterestRateBField().requestFocus();
			}
		} else if (e.getSource() == interestRateFieldB) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getApplyMortInsFieldB().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getAmortizationBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getInterestRateAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getInterestRateCField().requestFocus();
			}
		} else if (e.getSource() == interestRateFieldC) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getApplyMortInsFieldC().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getAmortizationCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getInterestRateBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAddressField().requestFocus();
			}
		} else if (e.getSource() == applyInsuranceBoxA) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getCostPerDoorField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getInterestRateAField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getRentField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getApplyMortInsFieldB().requestFocus();
			}
		} else if (e.getSource() == applyInsuranceBoxB) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getCostPerDoorField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getInterestRateBField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getApplyMortInsFieldA().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getApplyMortInsFieldC().requestFocus();
			}
		} else if (e.getSource() == applyInsuranceBoxC) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getCostPerDoorField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getInterestRateCField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getApplyMortInsFieldB().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getAddressField().requestFocus();
			}
		}
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			updateModel();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == loanAmountField
				&& loanAmountField.getBackground() != overriddenColor) {
			int response = JOptionPane
					.showConfirmDialog(
							this,
							"Would you like to override the Loan Amount for mortgage #1?",
							"Override Loan Amount (mort #1)?",
							JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				loanAmountField.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideLoanAmountA(true);

				// update JMenuCheckBox
				fm.getLoanOverrideA().setSelected(true);
			}
		} else if (e.getSource() == loanAmountFieldB
				&& loanAmountFieldB.getBackground() != overriddenColor) {
			int response = JOptionPane
					.showConfirmDialog(
							this,
							"Would you like to override the Loan Amount for mortgage #2?",
							"Override Loan Amount (mort #2)?",
							JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				loanAmountFieldB.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideLoanAmountB(true);

				// update JMenuCheckBox
				fm.getLoanOverrideB().setSelected(true);
			}
		} else if (e.getSource() == loanAmountFieldC
				&& loanAmountFieldC.getBackground() != overriddenColor) {
			int response = JOptionPane
					.showConfirmDialog(
							this,
							"Would you like to override the Loan Amount for mortgage #3?",
							"Override Loan Amount (mort #3)?",
							JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				loanAmountFieldC.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideLoanAmountC(true);

				// update JMenuCheckBox
				fm.getLoanOverrideC().setSelected(true);
			}
		} else if (e.getSource() == cmhcFeeField
				&& cmhcFeeField.getBackground() != overriddenColor) {
			int response = JOptionPane.showConfirmDialog(this,
					"Would you like to override the CMHC Fee for mortgage #1?",
					"Override CMHC Fee (mort #1)?", JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				cmhcFeeField.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideCMHCA(true);

				// update JMenuCheckBox
				fm.getCmhcOverrideA().setSelected(true);
			}
		} else if (e.getSource() == cmhcFeeFieldB
				&& cmhcFeeFieldB.getBackground() != overriddenColor) {
			int response = JOptionPane.showConfirmDialog(this,
					"Would you like to override the CMHC Fee for mortgage #2?",
					"Override CMHC Fee (mort #2)?", JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				cmhcFeeFieldB.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideCMHCB(true);

				// update JMenuCheckBox
				fm.getCmhcOverrideB().setSelected(true);
			}
		} else if (e.getSource() == cmhcFeeFieldC
				&& cmhcFeeFieldC.getBackground() != overriddenColor) {
			int response = JOptionPane.showConfirmDialog(this,
					"Would you like to override the CMHC Fee for mortgage #3?",
					"Override CMHC Fee (mort #3)?", JOptionPane.YES_NO_OPTION);
			// if response is 'yes'
			if (response == 0) {
				// change field color to overridden color
				cmhcFeeFieldC.setBackground(overriddenColor);

				// update DataModel flag
				dm.setOverrideCMHCC(true);

				// update JMenuCheckBox
				fm.getCmhcOverrideC().setSelected(true);
			}
		}

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {


	}

	@Override
	public void mousePressed(MouseEvent e) {


	}

	@Override
	public void mouseReleased(MouseEvent e) {


	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			updateModel();
		}
		
	}
}
